package cn.tips.dmp.biz.talent.wxApplet.service.impl;

import cn.tips.dmp.biz.talent.wxApplet.model.WxBO;
import cn.tips.dmp.biz.talent.wxApplet.service.WxCheckUserService;
import cn.tips.dmp.biz.talent.wxApplet.service.WxWebService;
import cn.tips.dmp.supports.result.ApiResultDTO;
import cn.tips.dmp.supports.result.ApiResultGenerator;
import cn.tips.dmp.system.user.dto.SystemUserDTO;
import cn.tips.dmp.system.user.mapper.SystemUserMapper;
import cn.tips.dmp.system.user.repository.SystemUserRepository;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class WxWebServiceImpl implements WxWebService {

    private final SystemUserRepository systemUserRepository;

    private final SystemUserMapper systemUserMapper;

    private final WxCheckUserService wxCheckUserService;

    private final RedisTemplate redisTemplate;

    private final JPAQueryFactory jpaQueryFactory;


    @Override
    public ApiResultDTO loginAndGetInfo(String mobile, WxBO wxBO) {
        ApiResultDTO apiResultDTO;

//        TipsWxUserDTO info = new TipsWxUserDTO();
//        info.setPhone(mobile);
//        try {
//
//        }catch (Exception e){
//            e.printStackTrace();
//            apiResultDTO = ApiResultGenerator.create("-1", "当前手机号存在多个用户记录，请联系管理员修改！!");
//            return apiResultDTO;
//        }

        return ApiResultGenerator.create("0", "数据获取成功!");
    }

    @Override
    public ApiResultDTO getToken(WxBO wxBO) {
        ApiResultDTO apiResultDTO;
//
//        //查找manager信息
//        TipsWxUserPO tipsWxUserPO = wxUserRepository.findByPhone(tipsWxBO.getPhoneNumber());
//        if(tipsWxUserPO == null) {
//            // 保存managerUser数据
//            TipsWxUserPO info = new TipsWxUserPO();
//            info.setPhone(tipsWxBO.getPhoneNumber());
//            info.setOpenId(tipsWxBO.getOpenid());
//            info.setUnionId(tipsWxBO.getUnionId());
//            userRepository.save(info);
//        }
//        String token;
//        try {
//            // 获取token并存储到redis
//            token = JwtUtil.generateToken(tipsWxBO.getPhoneNumber(), tipsWxBO.getOpenid(), tipsWxBO.getUnionId(), tipsWxBO.getLoginType());
//            redisTemplate.opsForValue().set(JwtUtil.JWT_SHIRO_CACHE_KEY_PREFIX + tipsWxBO.getPhoneNumber(), token, JwtUtil.MEMBER_EXPIRE_TIME, TimeUnit.SECONDS);
//        }catch (Exception e){
//            e.printStackTrace();
//            apiResultDTO = ApiResultGenerator.create("-1", "获取token失败");
//            return apiResultDTO;
//        }

        return ApiResultGenerator.create("0", "数据获取成功!");
    }

    @Override
    public void addWxUser(SystemUserDTO wxUserDTO) {
        systemUserRepository.save(systemUserMapper.dto2po(wxUserDTO));
    }

    @Override
    public ApiResultDTO compareTempExpertInfo(WxBO wxBO) {
        ApiResultDTO apiResultDTO;
        //添加待审核记录
        wxCheckUserService.saveWxCheckUserData(wxBO.getRealName(), wxBO.getPhoneNumber(), wxBO.getOrgId(), "1");

        apiResultDTO = ApiResultGenerator.create("-2", "请等待智库审核！");

        return apiResultDTO;
    }

}
